<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item>
        <label style="color: green;">同步/配置弘新能源电价方案</label>
      </el-form-item>
      <el-form-item label="方案名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入方案名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button
          type="success"
          plain
          @click="sync"
          ><Icon icon="ep:refresh" />同步电价方案</el-button
        >
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="关联外系统方案ID" align="center" prop="relaPlmId" />
      <el-table-column label="方案名称" align="left" prop="planName" min-width="220"/>
      <el-table-column label="适用范围" align="center" prop="applyFlag" width="100">
        <template #default="scope">
          <span style="color:red;" v-if="scope.row.applyFlag == 0">通用</span>
          <span style="color:green;" v-if="scope.row.applyFlag == 1">楼栋</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="90px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['zwsj:meter-price-plan:sync']"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MeterPricePlanForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { MeterPricePlanApi, MeterPricePlanVO } from '@/api/zwsj/meterpriceplan'
import MeterPricePlanForm from './MeterPricePlanForm.vue'

/** 电价方案 列表 */
defineOptions({ name: 'MeterPricePlan' })

const loading = ref(true) // 列表的加载中
const list = ref<MeterPricePlanVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  factoryCode: '1',
  applyFlag: undefined,
  planName: undefined,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MeterPricePlanApi.getMeterPricePlanPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/**同步方案 */
const sync = async () => {
  ElMessageBox.confirm('该操作将会同步电价方案信息，确定继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  })
    .then(() => {
      MeterPricePlanApi.syncHxnyMeterPricePlan().then( (data)  => {
        if(data){
          handleQuery()
        }
      })
    })
    .catch(() => {
    })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>